
/**************************************************START OF FILE*****************************************************/




/*------------------------------------------------------------------------------------------------------------------
Includes
*/
#include "xBsp_Hcs301.h"
#include "stm32f0xx_hal.h"
#include "stdint.h"
#include "xApp_GLB_Define.h"
#include "xSys_Hcs301.h"

/*------------------------------------------------------------------------------------------------------------------
Definitions
*/


/*------------------------------------------------------------------------------------------------------------------
Variables
*/

uint8_t USBD_HID_TxBuf[64];


/*------------------------------------------------------------------------------------------------------------------
Code
*/


extern uint8_t USBD_HID_SendReport(uint8_t *report, uint16_t len);

void USDB_HID_SendRfCode(uint32_t EncryPort , uint32_t FixedPort);
void USDB_HID_SendParaToPc(uint32_t DecryptKet_H , uint32_t DecryptKet_L , uint8_t LearnMode);



/*
*********************************************************************************************************************

@ Brief  : 应用部分的入口函数

@ Param  : NONE

@ Return : NONE

@ Author : YWJ(QQ:872180981)

@ Data   : 2020-08-04 15:35

*********************************************************************************************************************
*/
void XAPP_Main(void)
{


    xBSP_HCS2301_Init();

    while (1)
    {
        //返回无线接收到的数据
        if(IsSendCodeToPc)
        {
			IsSendCodeToPc = 0;
            USDB_HID_SendRfCode(gHcs301Code1 , gHcs301Code2);
        }

        //返回板子的一些当前参数
        if(gIsSendParaToPc)
        {
            uint32_t dataA , dataB;
			gIsSendParaToPc = 0;
            //密钥H
            dataA =  HCS301_Decryptkey[7];
            dataA <<= 8;
            dataA |=  HCS301_Decryptkey[6];
            dataA <<= 8;
            dataA |=  HCS301_Decryptkey[5];
            dataA <<= 8;
            dataA |=  HCS301_Decryptkey[4];
            //密钥L
            dataB =  HCS301_Decryptkey[3];
            dataB <<= 8;
            dataB |=  HCS301_Decryptkey[2];
            dataB <<= 8;
            dataB |=  HCS301_Decryptkey[1];
            dataB <<= 8;
            dataB |=  HCS301_Decryptkey[0];
            USDB_HID_SendParaToPc(dataA , dataB, 0x01);
        }


    }

}





/*
*********************************************************************************************************************

@ Brief  : 向PC发送从RF中接收到的数据

@ Param  : NONE

@ Return : NONE

@ Author : YWJ(QQ:872180981)

@ Data   : 2020-08-29 17:48

*********************************************************************************************************************
*/
void USDB_HID_SendRfCode(uint32_t EncryPort , uint32_t FixedPort)
{
    uint16_t CheckSum = 0;

    USBD_HID_TxBuf[0] = 0xAA;
    USBD_HID_TxBuf[1] = 0x55;
    USBD_HID_TxBuf[62] = 0x55;
    USBD_HID_TxBuf[63] = 0xAA;

    //命令字
    USBD_HID_TxBuf[2] = 0xF0;

    //数据
    USBD_HID_TxBuf[3] = (uint8_t)(EncryPort>>24);
    USBD_HID_TxBuf[4] = (uint8_t)(EncryPort>>16);
    USBD_HID_TxBuf[5] = (uint8_t)(EncryPort>>8);
    USBD_HID_TxBuf[6] = (uint8_t)(EncryPort>>0);
    //数据
    USBD_HID_TxBuf[7] = (uint8_t)(FixedPort>>24);
    USBD_HID_TxBuf[8] = (uint8_t)(FixedPort>>16);
    USBD_HID_TxBuf[9] = (uint8_t)(FixedPort>>8);
    USBD_HID_TxBuf[10] = (uint8_t)(FixedPort>>0);

    //CHECKSUM计算
    for (uint8_t  i = 0; i < 58; i++)
    {
        CheckSum += USBD_HID_TxBuf[2+i];
    }

    //CHECKSUM
    USBD_HID_TxBuf[60] = (uint8_t)(CheckSum>>8);
    USBD_HID_TxBuf[61] = (uint8_t)(CheckSum>>0);

    USBD_HID_SendReport(USBD_HID_TxBuf , 0x40);
}


/*
*********************************************************************************************************************

@ Brief  : 返回板子参数

@ Param  : DecryptKet ： 当前解码的密钥 LearnMode ： 当前的学习模式

@ Return : NONE

@ Author : YWJ(QQ:872180981)

@ Data   : 2020-08-29 22:30

*********************************************************************************************************************
*/
void USDB_HID_SendParaToPc(uint32_t DecryptKet_H , uint32_t DecryptKet_L , uint8_t LearnMode)
{
    uint16_t CheckSum = 0;

    USBD_HID_TxBuf[0] = 0xAA;
    USBD_HID_TxBuf[1] = 0x55;
    USBD_HID_TxBuf[62] = 0x55;
    USBD_HID_TxBuf[63] = 0xAA;

    //命令字
    USBD_HID_TxBuf[2] = 0xE0;

    //数据
    USBD_HID_TxBuf[3] = (uint8_t)(DecryptKet_H>>24);
    USBD_HID_TxBuf[4] = (uint8_t)(DecryptKet_H>>16);
    USBD_HID_TxBuf[5] = (uint8_t)(DecryptKet_H>>8);
    USBD_HID_TxBuf[6] = (uint8_t)(DecryptKet_H>>0);
    //数据
    USBD_HID_TxBuf[7] = (uint8_t)(DecryptKet_L>>24);
    USBD_HID_TxBuf[8] = (uint8_t)(DecryptKet_L>>16);
    USBD_HID_TxBuf[9] = (uint8_t)(DecryptKet_L>>8);
    USBD_HID_TxBuf[10] = (uint8_t)(DecryptKet_L>>0);

    //学习模式
    USBD_HID_TxBuf[11] = LearnMode;
    

    //CHECKSUM计算
    for (uint8_t  i = 0; i < 58; i++)
    {
        CheckSum += USBD_HID_TxBuf[2+i];
    }

    //CHECKSUM
    USBD_HID_TxBuf[60] = (uint8_t)(CheckSum>>8);
    USBD_HID_TxBuf[61] = (uint8_t)(CheckSum>>0);

    USBD_HID_SendReport(USBD_HID_TxBuf , 0x40);

}



/*
*********************************************************************************************************************

@ Brief  : USB接收数据中断处理函数

@ Param  : NONE

@ Return : NONE

@ Author : YWJ(QQ:872180981)

@ Data   : 2020-08-29 22:31

*********************************************************************************************************************
*/
void USBD_HID_RxData(uint8_t* RxData)
{

    uint16_t CheckSum = 0;
    uint16_t RxCheckSum = 0;
    //判断头跟尾
    if((RxData[0] == 0xAA)  && (RxData[1] == 0x55) && (RxData[62] == 0x55) && (RxData[63] == 0xAA))
    {
        //计算checksum
        for (int i = 0; i < 58; i++)
        {
            CheckSum += RxData[i+ 2];
        }

        //接收到的checksum
        RxCheckSum = RxData[60];
        RxCheckSum <<= 8;
        RxCheckSum |= RxData[61];
        
        if(RxCheckSum == CheckSum)
        {
            switch (RxData[2])
            {
                //读取当前板子参数
                case 0xF0:
                    gIsSendParaToPc = 1;
                break;
                //写入密钥
                case 0xF1:
                    for (uint8_t i = 0; i < 8; i++)
                    {
                        HCS301_Decryptkey[i]  = RxData[3 + i];
                    }
                break;
                //写入种子值在第三个安全的解密方式中要用到
                case 0xF2:
            
                break;
            }
        }
        

    }
}


/**************************************************END OF FILE*******************************************************/

